package FileUtils

import (
	"os"
	"errors"
	"bufio"
)

func CheckFileExist(path string)(bool){
	var _,err = os.Stat(path)
	if(!os.IsNotExist(err)){
		return true
	}else{
		return false
	}
}

func CheckFileIsExistAndIsDir(path string)(bool){
	var fileInfo,err = os.Stat(path)
	if(!os.IsNotExist(err)){
		if(fileInfo.IsDir()){
			return true
		}else{
			return false
		}
	}else{
		return false
	}
}
func CheckFileIsExistAndIsFile(path string)(bool){
	var fileInfo,err = os.Stat(path)
	if(!os.IsNotExist(err)){
		if(!fileInfo.IsDir()){
			return true
		}else{
			return false
		}
	}else{
		return false
	}
}
func ReadFile(path string)(string,error){
	if(!CheckFileIsExistAndIsFile(path)){
		return "",errors.New("文件不存在或非文件")
	}
	var f,err = os.OpenFile(path,os.O_APPEND,0666);
	if(err!=nil){
		return "",err
	}
	f.Seek(0,os.SEEK_SET)
	r := bufio.NewReader(f)
	var result = make([]byte,0,4096)
	var temp = make([]byte,4096)
	for true {
		i,err2 := r.Read(temp)
		//fmt.Println("读取了["+strconv.Itoa(i)+"]字节")
		if(err2!=nil){
			break
		}else{
			result = append(result,temp[:i]...)
		}
	}
	return string(result),nil
}
func ReadByteFile(path string)([]byte,error){
	if(!CheckFileIsExistAndIsFile(path)){
		return []byte(""),errors.New("文件不存在或非文件")
	}
	var f,err = os.OpenFile(path,os.O_APPEND,0666);
	if(err!=nil){
		return []byte(""),err
	}
	f.Seek(0,os.SEEK_SET)
	r := bufio.NewReader(f)
	var result = make([]byte,0,4096)
	var temp = make([]byte,4096)
	for true {
		i,err2 := r.Read(temp)
		//fmt.Println("读取了["+strconv.Itoa(i)+"]字节")
		if(err2!=nil){
			break
		}else{
			result = append(result,temp[:i]...)
		}
	}
	return result,nil
}
func GetFileSize(path string)(int64) {
	var fileInfo, err= os.Stat(path)
	if (!os.IsNotExist(err)) {
		if (!fileInfo.IsDir()) {
			return fileInfo.Size()
		} else {
			return -1
		}
	} else {
		return -1
	}
}

func CreateDirs(dirsname string)(bool,error){
	var err = os.MkdirAll(dirsname,0666)
	if(err!=nil){
		return false,err
	}else{
		return true,nil
	}
}

func GetPathFromFilePath(filepath string)(string){
	var filepath_slice = []byte(filepath)
	var lastSpeartorIndex = 0;
	for i:= len(filepath_slice)-1;i>=0;i-- {
		if(filepath_slice[i]=='\\'||filepath_slice[i]=='/'){
			lastSpeartorIndex = i;
			break;
		}
	}
	return string(filepath_slice[:lastSpeartorIndex])
}

func CreateMessageFile(message string,path string)(bool,error){
	var filepath = GetPathFromFilePath(path)
	var dirready,err = CreateDirs(filepath)
	if(!dirready){
		return dirready,err
	}
	if(!CheckFileExist(path)){
		//文件不存在
		var f,err = os.Create(path)
		if(err!=nil){
			return false,err
		}
		var w = bufio.NewWriter(f)
		w.WriteString(message)
		w.Flush()
		f.Close()
	}else{
		return false,errors.New("文件已存在")
	}
	return true,nil
}

func AppendMessageFile(message string,path string)(bool,error){
	var filepath = GetPathFromFilePath(path)
	var dirready,err = CreateDirs(filepath)
	if(!dirready){
		return dirready,err
	}
	if(!CheckFileExist(path)){
		//文件不存在
		var f,err = os.Create(path)
		if(err!=nil){
			return false,err
		}
		var w = bufio.NewWriter(f)
		w.WriteString(message)
		w.Flush()
		f.Close()
	}else{
		//文件存在
		var f,err = os.OpenFile(path,os.O_APPEND,0666)
		if(err!=nil){
			return false,err
		}
		var w = bufio.NewWriter(f)
		w.WriteString(message)
		w.Flush()
		f.Close()
	}
	return true,nil
}
func CreateByteFile(message []byte,path string)(bool,error){
	var filepath = GetPathFromFilePath(path)
	var dirready,err = CreateDirs(filepath)
	if(!dirready){
		return dirready,err
	}
	if(!CheckFileExist(path)){
		//文件不存在
		var f,err = os.Create(path)
		if(err!=nil){
			return false,err
		}
		var w = bufio.NewWriter(f)
		w.Write(message)
		w.Flush()
		f.Close()
	}else{
		return false,errors.New("文件已存在")
	}
	return true,nil
}

func AppendByteFile(message []byte,path string)(bool,error){
	var filepath = GetPathFromFilePath(path)
	var dirready,err = CreateDirs(filepath)
	if(!dirready){
		return dirready,err
	}
	if(!CheckFileExist(path)){
		//文件不存在
		var f,err = os.Create(path)
		if(err!=nil){
			return false,err
		}
		var w = bufio.NewWriter(f)
		w.Write(message)
		w.Flush()
		f.Close()
	}else{
		//文件存在
		var f,err = os.OpenFile(path,os.O_APPEND,0666)
		if(err!=nil){
			return false,err
		}
		var w = bufio.NewWriter(f)
		w.Write(message)
		w.Flush()
		f.Close()
	}
	return true,nil
}

func DeleteFile(path string)(bool,error){
	if(CheckFileIsExistAndIsFile(path)){
		var err = os.Remove(path)
		if(err==nil){
			return true,err
		}else{
			return false,nil
		}
	}else{
		return false,errors.New("非普通文件")
	}
}

func GetFileSeparator()(string){
	return "/"
}

